package ar.edu.itba.it.cg.grupo01.game.terrain.proctextures;


import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.terrain.util.AbstractHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;

public abstract class CustomProceduralTextureGenerator extends ProceduralTextureGenerator {


	public CustomProceduralTextureGenerator(AbstractHeightMap heightMap) {
		super(heightMap);
	}

	public TextureState getTextureState(){
		createTexture(512);

		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		
		Texture tx = TextureManager.loadTexture(getImageIcon().getImage(),
				MinificationFilter.BilinearNoMipMaps,
				MagnificationFilter.NearestNeighbor, true);

		ts.setTexture(tx);
		
		return ts;
	}
	public void createTexture(int textureSize) {
		super.createTexture(textureSize);

		BufferedImage img = new BufferedImage(textureSize, textureSize,
				BufferedImage.TYPE_INT_RGB);


		img.getGraphics().drawImage(proceduralTexture.getImage(), 0, 0, null);

		for (int x = 0; x < textureSize; x++) {
			for (int y = 0; y < textureSize; y++) {
				img.setRGB(x, y, getTextureColor(x,y));
			}
		}

		// create the new image from the data.
		proceduralTexture = new ImageIcon(img);
		proceduralTexture.setDescription("TerrainTexture");
	}

	protected abstract int getTextureColor(int x, int y);


}
